package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Note;
import cn.wolfcode.wolf2w.business.mapper.NoteMapper;
import cn.wolfcode.wolf2w.business.query.NoteCondition;
import cn.wolfcode.wolf2w.business.query.NoteQuery;
import cn.wolfcode.wolf2w.business.service.INoteService;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 旅游日记Service业务层处理
 *
 * @author ysc
 * @date 2024-12-17
 */
@Service
@Transactional
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements INoteService {
    @Autowired
    private RemoteUserInfoService remoteUserInfoService;
    @Autowired
    private INoteService noteService;

    @Override
    public IPage<Note> queryPage(NoteQuery qo) {
        IPage<Note> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        QueryWrapper<Note> qw = new QueryWrapper<>();
        qw.orderByDesc(qo.getOrderBy());

        NoteCondition dayCondition = NoteCondition.DAY_Map.get(qo.getDayType());
        if (dayCondition != null) {
            qw.between("days", dayCondition.getMin(), dayCondition.getMax());
        }
        NoteCondition consumeCondition = NoteCondition.CONSUME_Map.get(qo.getConsumeType());
        if (consumeCondition != null) {
            qw.between("avg_consume", consumeCondition.getMin(), consumeCondition.getMax());
        }

        NoteCondition timeCondition = NoteCondition.TIME_Map.get(qo.getTravelTimeType());
        if (timeCondition != null) {
            qw.between("MONTH(travel_time)", timeCondition.getMin(), timeCondition.getMax());
        }

        IPage<Note> pageResult = baseMapper.selectPage(page, qw);

        for (Note record : pageResult.getRecords()) {
            Long authorId = record.getAuthorId();
            UserInfo author = remoteUserInfoService.getOne(authorId, "inner").getData();
            record.setAuthor(author);
        }
        return pageResult;
    }

    @Override
    public List<Note> viewnnumTop3(Long destId) {

        return lambdaQuery().eq(Note::getDestId, destId)
                .orderByDesc(Note::getViewnum)
                .last("limit 3")
                .list();
    }
}
